package com.scuec.groupfive.mapper;

import com.scuec.groupfive.entity.PostComment;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface PostCommentMapper {
    /**
     * 获取总评论数
     */
    @Select("SELECT COUNT(*) FROM post_comments WHERE status = 1")
    int getTotalCommentCount();

    /**
     * 根据帖子ID获取评论数
     */
    @Select("SELECT COUNT(*) FROM post_comments WHERE post_id = #{postId} AND status = 1")
    int getCommentCountByPostId(Long postId);

    /**
     * 根据日期范围获取评论数
     */
    @Select("SELECT COUNT(*) FROM post_comments WHERE status = 1 AND created_at BETWEEN #{startDate} AND #{endDate}")
    int getCommentCountByDateRange(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    /**
     * 根据帖子ID查询评论列表（带分页）
     */
    @Select("SELECT " +
            "pc.comment_id AS commentId, " +
            "pc.post_id AS postId, " +
            "pc.user_id AS userId, " +
            "pc.content, " +
            "pc.parent_id AS parentId, " +
            "pc.status, " +
            "pc.created_at AS createTime, " +
            "pc.updated_at AS updateTime, " +
            "u.username " +
            "FROM post_comments pc " +
            "LEFT JOIN users u ON pc.user_id = u.user_id " +
            "WHERE pc.post_id = #{postId} AND pc.status = 1 " +
            "ORDER BY pc.created_at DESC " +
            "LIMIT #{offset}, #{size}")
    List<PostComment> getCommentsByPostId(
            @Param("postId") Long postId,
            @Param("offset") int offset,
            @Param("size") int size
    );

    /**
     * 逻辑删除评论
     */
    @Update("UPDATE post_comments SET status = 0 WHERE comment_id = #{commentId}")
    int deleteComment(@Param("commentId") Long commentId);

    /**
     * 根据帖子ID批量删除评论（逻辑删除）
     */
    @Update("UPDATE post_comments SET status = 0 WHERE post_id = #{postId}")
    int deleteCommentsByPostId(@Param("postId") Long postId);
    @Update("UPDATE post_comments SET status = 1 WHERE comment_id = #{commentId}")
    int restoreComment(Long commentId);

}